<?php

/**
 * Class YUserLoginBehavior
 */
class YUserLoginBehavior extends CBehavior
{
	public $userModel = null;

	public function events()
	{
		return array(
			'onAfterLogin' => 'afterLogin',
		);
	}

	/**
	 * @param CEvent $event
	 */
	public function afterLogin($event)
	{
		$user = $event->sender;
		if (!($user instanceof CWebUser) || !isset($this->userModel)) {
			return;
		}

		if (!isset($event->params['fromCookie']) || $event->params['fromCookie']) {
			return;
		}

		Yii::app()->watchdog->write('user_login_success', '%username 登录成功', array('%username'=>$user->name));
		$model = CActiveRecord::model($this->userModel);
		if ($model->hasAttribute('login_time')) {
			$model->updateByPk($user->id, array(
				'login_time' => time()
			));
		}
	}
}
